package Tests
{
	import Box2D.Collision.Shapes.b2CircleShape;
	import Box2D.Collision.Shapes.b2LoopShape;
	import Box2D.Collision.Shapes.b2PolygonShape;
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Common.Math.b2Vec2Array;
	import Box2D.Dynamics.Joints.b2RevoluteJoint;
	import Box2D.Dynamics.Joints.b2RevoluteJointDef;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2BodyDef;
	import Box2D.Dynamics.b2BodyType;
	import Box2D.Dynamics.b2FixtureDef;
	
	import Framework.Test;
	
	public class PinBall extends Test
	{
		private var ground:b2Body;
		private var m_leftJoint:b2RevoluteJoint;
		private var m_rightJoint:b2RevoluteJoint;
		private var m_ball:b2Body;
		private var m_button:Boolean
		
		public function PinBall()
		{
			super();
			createGround()
			createFlippers();
			createBall()
		}
		private function createGround():void{
			var bd:b2BodyDef = new b2BodyDef;
			ground = m_world.CreateBody(bd);			

			var vs:b2Vec2Array = new b2Vec2Array(5);
			vs.setAt(0,new b2Vec2(0,-2));
			vs.setAt(1,new b2Vec2(8,6));
			vs.setAt(2,new b2Vec2(8,20));
			vs.setAt(3,new b2Vec2(-8,20));
			vs.setAt(4,new b2Vec2(-8,6));
			
			var loop:b2LoopShape = new b2LoopShape;
			loop.Create(vs,vs.length);
			
			var fd:b2FixtureDef = new b2FixtureDef;
			fd.shape = loop;
			fd.density = 0;
			ground.CreateFixture(fd)
		}
		private function createFlippers():void{
			var p1:b2Vec2 = new b2Vec2(-2.0, 0.0);
			var p2:b2Vec2 = new b2Vec2(2.0, 0.0);
			
			var bd:b2BodyDef = new b2BodyDef;
			bd.type = b2BodyType.b2_dynamicBody;
			
			bd.position = p1;
			var leftFlipper:b2Body = m_world.CreateBody(bd);
			
			bd.position = p2;
			var rightFlipper:b2Body = m_world.CreateBody(bd);
			
			var box:b2PolygonShape = new b2PolygonShape;
			box.SetAsBox(1.75, 0.1);
			
			var fd:b2FixtureDef = new b2FixtureDef;
			fd.shape = box;
			fd.density = 1.0;
			
			leftFlipper.CreateFixture(fd);
			rightFlipper.CreateFixture(fd);
			
			var jd:b2RevoluteJointDef = new b2RevoluteJointDef;
			jd.bodyA = ground;
			jd.localAnchorB.SetZero();
			jd.enableMotor = true;
			jd.maxMotorTorque = 1000.0;
			jd.enableLimit = true;
			
			jd.motorSpeed = 0.0;
			jd.localAnchorA = p1;
			jd.bodyB = leftFlipper;
			jd.lowerAngle = -30.0 * Math.PI / 180.0;
			jd.upperAngle = 5.0 * Math.PI / 180.0;
			m_leftJoint = m_world.CreateJoint(jd) as b2RevoluteJoint;
			
			jd.motorSpeed = 0.0;
			jd.localAnchorA = p2;
			jd.bodyB = rightFlipper;
			jd.lowerAngle = -5.0 * Math.PI / 180.0;
			jd.upperAngle = 30.0 * Math.PI / 180.0;
			m_rightJoint = m_world.CreateJoint(jd) as b2RevoluteJoint;			
		}
		private function createBall():void{
			var bd:b2BodyDef = new b2BodyDef
			bd.position.Set(1.0, 15.0);
			bd.type = b2BodyType.b2_dynamicBody;
			bd.bullet = true;
			
			m_ball = m_world.CreateBody(bd);
			
			var shape:b2CircleShape = new b2CircleShape;
			shape.radius = 0.2;
			
			var fd:b2FixtureDef = new b2FixtureDef;
			fd.shape = shape;
			fd.density = 1.0;
			m_ball.CreateFixture(fd);			
		}
		public override function Key(code:String):void
		{
			switch(code)
			{
				case 'a':
					m_button = true;
					break;
			}
		}
		public override function KeyUp(code:String):void{
			switch(code){
				case 'a':
					m_button = false;
					break;
			}
		}
		public override function Step():void
		{
			if(m_button){
				m_leftJoint.SetMotorSpeed(20);
				m_rightJoint.SetMotorSpeed(-20);
			}
			else{
				m_leftJoint.SetMotorSpeed(-10);
				m_rightJoint.SetMotorSpeed(10);
			}
			super.Step();
		}
	}
}